<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2">
<meta name="theme-color" content="#222">
<meta name="generator" content="Hexo 5.1.1">
  <link rel="apple-touch-icon" sizes="180x180" href="/hexo/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/hexo/images/favicon-32x32-next.png">
  <link rel="icon" type="image/png" sizes="16x16" href="/hexo/images/favicon-16x16-next.png">
  <link rel="mask-icon" href="/hexo/images/logo.svg" color="#222">

<link rel="stylesheet" href="/hexo/css/main.css">


<link rel="stylesheet" href="/hexo/lib/font-awesome/css/all.min.css">

<script id="hexo-configurations">
    var NexT = window.NexT || {};
    var CONFIG = {"hostname":"youchen12138.gitee.io","root":"/hexo/","scheme":"Gemini","version":"7.8.0","exturl":false,"sidebar":{"position":"left","display":"post","padding":18,"offset":12,"onmobile":false},"copycode":{"enable":false,"show_result":false,"style":null},"back2top":{"enable":true,"sidebar":false,"scrollpercent":false},"bookmark":{"enable":false,"color":"#222","save":"auto"},"fancybox":false,"mediumzoom":false,"lazyload":false,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},"algolia":{"hits":{"per_page":10},"labels":{"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}},"localsearch":{"enable":false,"trigger":"auto","top_n_per_article":1,"unescape":false,"preload":false},"motion":{"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}}};
  </script>

  <meta name="description" content="ReactDOM.render12345678910111213141516171819202122232425262728const ReactDOM: Object &#x3D; &amp;#123;  &#x2F;&#x2F; 服务端渲染  hydrate(element: React$Node, container: DOMContainer, callback: ?Function) &amp;#123;    &#x2F;&#x2F; TODO: t">
<meta property="og:type" content="article">
<meta property="og:title" content="React源码-ReactDOM.render解析">
<meta property="og:url" content="https://youchen12138.gitee.io/hexo/2020/09/14/React%E6%BA%90%E7%A0%81-ReactDOM-render%E8%A7%A3%E6%9E%90/index.html">
<meta property="og:site_name" content="幽辰的博客">
<meta property="og:description" content="ReactDOM.render12345678910111213141516171819202122232425262728const ReactDOM: Object &#x3D; &amp;#123;  &#x2F;&#x2F; 服务端渲染  hydrate(element: React$Node, container: DOMContainer, callback: ?Function) &amp;#123;    &#x2F;&#x2F; TODO: t">
<meta property="og:locale" content="en_US">
<meta property="article:published_time" content="2020-09-14T01:36:49.000Z">
<meta property="article:modified_time" content="2020-09-14T02:20:58.214Z">
<meta property="article:author" content="幽辰">
<meta property="article:tag" content="React">
<meta property="article:tag" content="源码">
<meta name="twitter:card" content="summary">

<link rel="canonical" href="https://youchen12138.gitee.io/hexo/2020/09/14/React%E6%BA%90%E7%A0%81-ReactDOM-render%E8%A7%A3%E6%9E%90/">


<script id="page-configurations">
  // https://hexo.io/docs/variables.html
  CONFIG.page = {
    sidebar: "",
    isHome : false,
    isPost : true,
    lang   : 'en'
  };
</script>

  <title>React源码-ReactDOM.render解析 | 幽辰的博客</title>
  






  <noscript>
  <style>
  .use-motion .brand,
  .use-motion .menu-item,
  .sidebar-inner,
  .use-motion .post-block,
  .use-motion .pagination,
  .use-motion .comments,
  .use-motion .post-header,
  .use-motion .post-body,
  .use-motion .collection-header { opacity: initial; }

  .use-motion .site-title,
  .use-motion .site-subtitle {
    opacity: initial;
    top: initial;
  }

  .use-motion .logo-line-before i { left: initial; }
  .use-motion .logo-line-after i { right: initial; }
  </style>
</noscript>

</head>

<body itemscope itemtype="http://schema.org/WebPage">
  <div class="container use-motion">
    <div class="headband"></div>

    <header class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="Toggle navigation bar">
      <span class="toggle-line toggle-line-first"></span>
      <span class="toggle-line toggle-line-middle"></span>
      <span class="toggle-line toggle-line-last"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/hexo/" class="brand" rel="start">
      <span class="logo-line-before"><i></i></span>
      <h1 class="site-title">幽辰的博客</h1>
      <span class="logo-line-after"><i></i></span>
    </a>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger">
    </div>
  </div>
</div>




<nav class="site-nav">
  <ul id="menu" class="main-menu menu">
        <li class="menu-item menu-item-home">

    <a href="/hexo/" rel="section"><i class="fa fa-home fa-fw"></i>Home</a>

  </li>
        <li class="menu-item menu-item-tags">

    <a href="/hexo/tags/" rel="section"><i class="fa fa-tags fa-fw"></i>Tags<span class="badge">23</span></a>

  </li>
        <li class="menu-item menu-item-categories">

    <a href="/hexo/categories/" rel="section"><i class="fa fa-th fa-fw"></i>Categories<span class="badge">14</span></a>

  </li>
        <li class="menu-item menu-item-archives">

    <a href="/hexo/archives/" rel="section"><i class="fa fa-archive fa-fw"></i>Archives<span class="badge">127</span></a>

  </li>
  </ul>
</nav>




</div>
    </header>

    
  <div class="back-to-top">
    <i class="fa fa-arrow-up"></i>
    <span>0%</span>
  </div>


    <main class="main">
      <div class="main-inner">
        <div class="content-wrap">
          

          <div class="content post posts-expand">
            

    
  
  
  <article itemscope itemtype="http://schema.org/Article" class="post-block" lang="en">
    <link itemprop="mainEntityOfPage" href="https://youchen12138.gitee.io/hexo/2020/09/14/React%E6%BA%90%E7%A0%81-ReactDOM-render%E8%A7%A3%E6%9E%90/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/hexo/images/avatar.jpg">
      <meta itemprop="name" content="幽辰">
      <meta itemprop="description" content="">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="幽辰的博客">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          React源码-ReactDOM.render解析
        </h1>

        <div class="post-meta">
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="far fa-calendar"></i>
              </span>
              <span class="post-meta-item-text">Posted on</span>
              

              <time title="Created: 2020-09-14 09:36:49 / Modified: 10:20:58" itemprop="dateCreated datePublished" datetime="2020-09-14T09:36:49+08:00">2020-09-14</time>
            </span>
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="far fa-folder"></i>
              </span>
              <span class="post-meta-item-text">In</span>
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/hexo/categories/React/" itemprop="url" rel="index"><span itemprop="name">React</span></a>
                </span>
            </span>

          

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">

      
        <h3 id="ReactDOM-render"><a href="#ReactDOM-render" class="headerlink" title="ReactDOM.render"></a>ReactDOM.render</h3><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> ReactDOM: <span class="built_in">Object</span> = &#123;</span><br><span class="line">  <span class="comment">// 服务端渲染</span></span><br><span class="line">  hydrate(element: React$Node, <span class="attr">container</span>: DOMContainer, <span class="attr">callback</span>: ?<span class="built_in">Function</span>) &#123;</span><br><span class="line">    <span class="comment">// <span class="doctag">TODO:</span> throw or warn if we couldn&#x27;t hydrate?</span></span><br><span class="line">    <span class="keyword">return</span> legacyRenderSubtreeIntoContainer(</span><br><span class="line">      <span class="literal">null</span>,</span><br><span class="line">      element,</span><br><span class="line">      container,</span><br><span class="line">      <span class="literal">true</span>,</span><br><span class="line">      callback,</span><br><span class="line">    );</span><br><span class="line">  &#125;,</span><br><span class="line"></span><br><span class="line">  <span class="comment">// 客户端渲染</span></span><br><span class="line">  render(</span><br><span class="line">    element: React$Element&lt;any&gt;,</span><br><span class="line">    container: DOMContainer,</span><br><span class="line">    callback: ?<span class="built_in">Function</span>,</span><br><span class="line">  ) &#123;</span><br><span class="line">    <span class="keyword">return</span> legacyRenderSubtreeIntoContainer(</span><br><span class="line">      <span class="literal">null</span>,</span><br><span class="line">      element,</span><br><span class="line">      container,</span><br><span class="line">      <span class="literal">false</span>,</span><br><span class="line">      callback,</span><br><span class="line">    );</span><br><span class="line">  &#125;,</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p><code>hydrate</code> 和 <code>render</code> 最后都会调用 <code>legacyRenderSubtreeIntoContainer</code> 方法，区别就是第四个参数不同</p>
<p>在 <code>legacyRenderSubtreeIntoContainer</code>  的第四个参数是判断其是否是服务器端渲染</p>
<ul>
<li><code>forceHydrate</code> 为 <code>true</code>：服务器端渲染，在服务端尽可能复用节点，提高性能;</li>
<li><code>forceHydrate</code> 为 <code>false</code>：客户端渲染，浏览器端不会去复用节点，而是全部替换掉。</li>
</ul>
<h4 id="1、legacyRenderSubtreeIntoContainer"><a href="#1、legacyRenderSubtreeIntoContainer" class="headerlink" title="1、legacyRenderSubtreeIntoContainer"></a>1、legacyRenderSubtreeIntoContainer</h4><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// null, element, container, false, callback</span></span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">legacyRenderSubtreeIntoContainer</span>(<span class="params"></span></span></span><br><span class="line"><span class="function"><span class="params">  parentComponent: ?React$Component&lt;any, any&gt;,</span></span></span><br><span class="line"><span class="function"><span class="params">  children: ReactNodeList,</span></span></span><br><span class="line"><span class="function"><span class="params">  container: DOMContainer,</span></span></span><br><span class="line"><span class="function"><span class="params">  forceHydrate: boolean, <span class="regexp">//</span> 判断渲染的方式</span></span></span><br><span class="line"><span class="function"><span class="params">  callback: ?Function,</span></span></span><br><span class="line"><span class="function"><span class="params"></span>) </span>&#123;</span><br><span class="line">  <span class="comment">// <span class="doctag">TODO:</span> Ensure all entry points contain this check</span></span><br><span class="line">  <span class="comment">// 代码的错误提示</span></span><br><span class="line">  invariant(</span><br><span class="line">    isValidContainer(container),</span><br><span class="line">    <span class="string">&#x27;Target container is not a DOM element.&#x27;</span>,</span><br><span class="line">  );</span><br><span class="line"></span><br><span class="line">  <span class="comment">// 开发环境的提示，可忽略，下面我将所有__DEV__的代码删除，以避免阅读</span></span><br><span class="line">  <span class="keyword">if</span> (__DEV__) &#123;</span><br><span class="line">    topLevelUpdateWarnings(container);</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  <span class="comment">// <span class="doctag">TODO:</span> Without `any` type, Flow says &quot;Property cannot be accessed on any</span></span><br><span class="line">  <span class="comment">// member of intersection type.&quot; Whyyyyyy.</span></span><br><span class="line">  <span class="comment">// 由于在render渲染中一般都是DOM节点，所以container._reactRootContainer一般为null</span></span><br><span class="line">  <span class="keyword">let</span> root: Root = (container._reactRootContainer: any);</span><br><span class="line">  <span class="comment">// 所以一定会进入下面的代码</span></span><br><span class="line">  <span class="keyword">if</span> (!root) &#123;</span><br><span class="line">    <span class="comment">// Initial mount</span></span><br><span class="line">    <span class="comment">// 相当于创建一个ReactRoot，下面会有讲解</span></span><br><span class="line">    root = container._reactRootContainer = legacyCreateRootFromDOMContainer(</span><br><span class="line">      container,</span><br><span class="line">      forceHydrate,</span><br><span class="line">    );</span><br><span class="line">    </span><br><span class="line">    <span class="comment">// 判断callback是否为函数，如果是函数，则调用回调</span></span><br><span class="line">    <span class="keyword">if</span> (<span class="keyword">typeof</span> callback === <span class="string">&#x27;function&#x27;</span>) &#123;</span><br><span class="line">      <span class="keyword">const</span> originalCallback = callback;</span><br><span class="line">      callback = <span class="function"><span class="keyword">function</span>(<span class="params"></span>) </span>&#123;</span><br><span class="line">        <span class="comment">//根据fiberRoot获取公共Root实例，fiberRoot会在后面的文章里会介绍</span></span><br><span class="line">        <span class="comment">//就是fiberRoot.current.child.stateNode</span></span><br><span class="line">        <span class="keyword">const</span> instance = DOMRenderer.getPublicRootInstance(root._internalRoot);</span><br><span class="line">        <span class="comment">//通过该实例instance 去调用originalCallback方法</span></span><br><span class="line">        originalCallback.call(instance);</span><br><span class="line">      &#125;;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="comment">// Initial mount should not be batched.</span></span><br><span class="line">    <span class="comment">// 初次渲染不会批量更新</span></span><br><span class="line">    DOMRenderer.unbatchedUpdates(<span class="function">() =&gt;</span> &#123;</span><br><span class="line">      <span class="comment">// 由传入的参数可以知道parentComponent为null，一定不会调用下面的方法</span></span><br><span class="line">      <span class="keyword">if</span> (parentComponent != <span class="literal">null</span>) &#123;</span><br><span class="line">        root.legacy_renderSubtreeIntoContainer(</span><br><span class="line">          parentComponent,</span><br><span class="line">          children,</span><br><span class="line">          callback,</span><br><span class="line">        );</span><br><span class="line">      &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">        <span class="comment">// 所以该方法主要执行的就是ReactRoot的render方法</span></span><br><span class="line">        root.render(children, callback);</span><br><span class="line">      &#125;</span><br><span class="line">    &#125;);</span><br><span class="line">  &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">    <span class="keyword">if</span> (<span class="keyword">typeof</span> callback === <span class="string">&#x27;function&#x27;</span>) &#123;</span><br><span class="line">      <span class="keyword">const</span> originalCallback = callback;</span><br><span class="line">      callback = <span class="function"><span class="keyword">function</span>(<span class="params"></span>) </span>&#123;</span><br><span class="line">        <span class="keyword">const</span> instance = DOMRenderer.getPublicRootInstance(root._internalRoot);</span><br><span class="line">        originalCallback.call(instance);</span><br><span class="line">      &#125;;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="comment">// Update</span></span><br><span class="line">    <span class="keyword">if</span> (parentComponent != <span class="literal">null</span>) &#123;</span><br><span class="line">      root.legacy_renderSubtreeIntoContainer(</span><br><span class="line">        parentComponent,</span><br><span class="line">        children,</span><br><span class="line">        callback,</span><br><span class="line">      );</span><br><span class="line">    &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">      root.render(children, callback);</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;</span><br><span class="line">  <span class="keyword">return</span> DOMRenderer.getPublicRootInstance(root._internalRoot);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h4 id="1-1-legacyCreateRootFromDOMContainer"><a href="#1-1-legacyCreateRootFromDOMContainer" class="headerlink" title="1.1 legacyCreateRootFromDOMContainer"></a>1.1 legacyCreateRootFromDOMContainer</h4><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// container, false</span></span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">legacyCreateRootFromDOMContainer</span>(<span class="params"></span></span></span><br><span class="line"><span class="function"><span class="params">  container: DOMContainer,</span></span></span><br><span class="line"><span class="function"><span class="params">  forceHydrate: boolean,</span></span></span><br><span class="line"><span class="function"><span class="params"></span>): <span class="title">Root</span> </span>&#123;</span><br><span class="line">  <span class="comment">// 因为forceHydrate为false，所有需要调用shouldHydrateDueToLegacyHeuristic判断其是否为服务器端渲染</span></span><br><span class="line">  <span class="keyword">const</span> shouldHydrate =</span><br><span class="line">    forceHydrate || shouldHydrateDueToLegacyHeuristic(container);</span><br><span class="line">  <span class="comment">// First clear any existing content.</span></span><br><span class="line">  <span class="comment">// 如果是false，则为客户端渲染</span></span><br><span class="line">  <span class="keyword">if</span> (!shouldHydrate) &#123;</span><br><span class="line">    <span class="keyword">let</span> warned = <span class="literal">false</span>;</span><br><span class="line">    <span class="keyword">let</span> rootSibling;</span><br><span class="line">    <span class="comment">// 遍历以删除container下所有的子节点</span></span><br><span class="line">    <span class="keyword">while</span> ((rootSibling = container.lastChild)) &#123;</span><br><span class="line">      container.removeChild(rootSibling);</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;</span><br><span class="line">  <span class="comment">// Legacy roots are not async by default.</span></span><br><span class="line">  <span class="keyword">const</span> isConcurrent = <span class="literal">false</span>;</span><br><span class="line">  <span class="comment">// 由此我们可以看到这是创建了一个ReactRoot</span></span><br><span class="line">  <span class="keyword">return</span> <span class="keyword">new</span> ReactRoot(container, isConcurrent, shouldHydrate);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h4 id="1-1-1-shouldHydrateDueToLegacyHeuristic"><a href="#1-1-1-shouldHydrateDueToLegacyHeuristic" class="headerlink" title="1.1.1 shouldHydrateDueToLegacyHeuristic"></a>1.1.1 shouldHydrateDueToLegacyHeuristic</h4><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">shouldHydrateDueToLegacyHeuristic</span>(<span class="params">container</span>) </span>&#123;</span><br><span class="line">  <span class="comment">// 获取container的第一个节点</span></span><br><span class="line">  <span class="keyword">const</span> rootElement = getReactRootElementInContainer(container);</span><br><span class="line">  <span class="keyword">return</span> !!(</span><br><span class="line">    rootElement &amp;&amp;</span><br><span class="line">    rootElement.nodeType === ELEMENT_NODE &amp;&amp;</span><br><span class="line">    <span class="comment">// 获取container的第一个节点，判断其是否有ROOT_ATTRIBUTE_NAME属性，如果有，则为服务器端渲染</span></span><br><span class="line">    rootElement.hasAttribute(ROOT_ATTRIBUTE_NAME)</span><br><span class="line">  );</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h4 id="1-1-1-1-ROOT-ATTRIBUTE-NAME"><a href="#1-1-1-1-ROOT-ATTRIBUTE-NAME" class="headerlink" title="1.1.1.1 ROOT_ATTRIBUTE_NAME"></a>1.1.1.1 ROOT_ATTRIBUTE_NAME</h4><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//服务端渲染的话，会在React App的第一个元素上添加该属性</span></span><br><span class="line"><span class="comment">//以标识是服务端渲染的</span></span><br><span class="line"><span class="keyword">export</span> <span class="keyword">const</span> ROOT_ATTRIBUTE_NAME = <span class="string">&#x27;data-reactroot&#x27;</span>;</span><br></pre></td></tr></table></figure>

<h4 id="1-2-getPublicRootInstance"><a href="#1-2-getPublicRootInstance" class="headerlink" title="1.2 getPublicRootInstance"></a>1.2 getPublicRootInstance</h4><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">export</span> <span class="function"><span class="keyword">function</span> <span class="title">getPublicRootInstance</span>(<span class="params"></span></span></span><br><span class="line"><span class="function"><span class="params">  container: OpaqueRoot,</span></span></span><br><span class="line"><span class="function"><span class="params"></span>): <span class="title">React$Component</span>&lt;<span class="title">any</span>, <span class="title">any</span>&gt; | <span class="title">PublicInstance</span> | <span class="title">null</span> </span>&#123;</span><br><span class="line">  <span class="keyword">const</span> containerFiber = container.current;</span><br><span class="line">  <span class="comment">// 因为没有子节点，所有render方法一定是返回null</span></span><br><span class="line">  <span class="keyword">if</span> (!containerFiber.child) &#123;</span><br><span class="line">    <span class="keyword">return</span> <span class="literal">null</span>;</span><br><span class="line">  &#125;</span><br><span class="line">  <span class="keyword">switch</span> (containerFiber.child.tag) &#123;</span><br><span class="line">    <span class="keyword">case</span> HostComponent:</span><br><span class="line">      <span class="keyword">return</span> getPublicInstance(containerFiber.child.stateNode);</span><br><span class="line">    <span class="keyword">default</span>:</span><br><span class="line">      <span class="keyword">return</span> containerFiber.child.stateNode;</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h4 id="1-3-ReactRoot-render"><a href="#1-3-ReactRoot-render" class="headerlink" title="1.3 ReactRoot.render"></a>1.3 ReactRoot.render</h4><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line">ReactRoot.prototype.render = <span class="function"><span class="keyword">function</span>(<span class="params"></span></span></span><br><span class="line"><span class="function"><span class="params">  children: ReactNodeList,</span></span></span><br><span class="line"><span class="function"><span class="params">  callback: ?() =&gt; mixed,</span></span></span><br><span class="line"><span class="function"><span class="params"></span>): <span class="title">Work</span> </span>&#123;</span><br><span class="line">  <span class="keyword">const</span> root = <span class="built_in">this</span>._internalRoot;</span><br><span class="line">  <span class="keyword">const</span> work = <span class="keyword">new</span> ReactWork();</span><br><span class="line">  callback = callback === <span class="literal">undefined</span> ? <span class="literal">null</span> : callback;</span><br><span class="line">  <span class="keyword">if</span> (callback !== <span class="literal">null</span>) &#123;</span><br><span class="line">    work.then(callback);</span><br><span class="line">  &#125;</span><br><span class="line">  DOMRenderer.updateContainer(children, root, <span class="literal">null</span>, work._onCommit);</span><br><span class="line">  <span class="keyword">return</span> work;</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>

<h4 id="1-3-1-updateContainer"><a href="#1-3-1-updateContainer" class="headerlink" title="1.3.1 updateContainer"></a>1.3.1 updateContainer</h4><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// children, root, null, word._onCommit</span></span><br><span class="line"><span class="keyword">export</span> <span class="function"><span class="keyword">function</span> <span class="title">updateContainer</span>(<span class="params"></span></span></span><br><span class="line"><span class="function"><span class="params">  element: ReactNodeList,</span></span></span><br><span class="line"><span class="function"><span class="params">  container: OpaqueRoot,</span></span></span><br><span class="line"><span class="function"><span class="params">  parentComponent: ?React$Component&lt;any, any&gt;,</span></span></span><br><span class="line"><span class="function"><span class="params">  callback: ?Function,</span></span></span><br><span class="line"><span class="function"><span class="params"></span>): <span class="title">ExpirationTime</span> </span>&#123;</span><br><span class="line">  <span class="keyword">const</span> current = container.current;</span><br><span class="line">  <span class="comment">// 计算新开始的时间</span></span><br><span class="line">  <span class="keyword">const</span> currentTime = requestCurrentTime();</span><br><span class="line">  </span><br><span class="line">  <span class="comment">// 每到过期时间，react就会更新 container</span></span><br><span class="line">  <span class="keyword">const</span> expirationTime = computeExpirationForFiber(currentTime, current);</span><br><span class="line">    </span><br><span class="line">  <span class="keyword">return</span> updateContainerAtExpirationTime(</span><br><span class="line">    element,</span><br><span class="line">    container,</span><br><span class="line">    parentComponent,</span><br><span class="line">    expirationTime,</span><br><span class="line">    callback,</span><br><span class="line">  );</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h4 id="1-3-1-1-updateContainerAtExpirationTime"><a href="#1-3-1-1-updateContainerAtExpirationTime" class="headerlink" title="1.3.1.1 updateContainerAtExpirationTime"></a>1.3.1.1 updateContainerAtExpirationTime</h4><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">export</span> <span class="function"><span class="keyword">function</span> <span class="title">updateContainerAtExpirationTime</span>(<span class="params"></span></span></span><br><span class="line"><span class="function"><span class="params">  element: ReactNodeList,</span></span></span><br><span class="line"><span class="function"><span class="params">  container: OpaqueRoot,</span></span></span><br><span class="line"><span class="function"><span class="params">  parentComponent: ?React$Component&lt;any, any&gt;,</span></span></span><br><span class="line"><span class="function"><span class="params">  expirationTime: ExpirationTime,</span></span></span><br><span class="line"><span class="function"><span class="params">  callback: ?Function,</span></span></span><br><span class="line"><span class="function"><span class="params"></span>) </span>&#123;</span><br><span class="line">  <span class="comment">// <span class="doctag">TODO:</span> If this is a nested container, this won&#x27;t be the root.</span></span><br><span class="line">  <span class="keyword">const</span> current = container.current;</span><br><span class="line"></span><br><span class="line">  <span class="keyword">const</span> context = getContextForSubtree(parentComponent);</span><br><span class="line">  <span class="keyword">if</span> (container.context === <span class="literal">null</span>) &#123;</span><br><span class="line">    container.context = context;</span><br><span class="line">  &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">    container.pendingContext = context;</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  <span class="comment">// 计划更新root</span></span><br><span class="line">  <span class="keyword">return</span> scheduleRootUpdate(current, element, expirationTime, callback);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h4 id="1-3-1-1-1-scheduleRootUpdate"><a href="#1-3-1-1-1-scheduleRootUpdate" class="headerlink" title="1.3.1.1.1 scheduleRootUpdate"></a>1.3.1.1.1 scheduleRootUpdate</h4><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">scheduleRootUpdate</span>(<span class="params"></span></span></span><br><span class="line"><span class="function"><span class="params">  current: Fiber,</span></span></span><br><span class="line"><span class="function"><span class="params">  element: ReactNodeList,</span></span></span><br><span class="line"><span class="function"><span class="params">  expirationTime: ExpirationTime,</span></span></span><br><span class="line"><span class="function"><span class="params">  callback: ?Function,</span></span></span><br><span class="line"><span class="function"><span class="params"></span>) </span>&#123;</span><br><span class="line">  <span class="keyword">const</span> update = createUpdate(expirationTime); <span class="comment">// 标记react更新的点</span></span><br><span class="line">  <span class="comment">// Caution: React DevTools currently depends on this property</span></span><br><span class="line">  <span class="comment">// being called &quot;element&quot;.</span></span><br><span class="line">  update.payload = &#123;element&#125;;</span><br><span class="line"></span><br><span class="line">  callback = callback === <span class="literal">undefined</span> ? <span class="literal">null</span> : callback;</span><br><span class="line">  <span class="keyword">if</span> (callback !== <span class="literal">null</span>) &#123;</span><br><span class="line">    warningWithoutStack(</span><br><span class="line">      <span class="keyword">typeof</span> callback === <span class="string">&#x27;function&#x27;</span>,</span><br><span class="line">      <span class="string">&#x27;render(...): Expected the last optional `callback` argument to be a &#x27;</span> +</span><br><span class="line">        <span class="string">&#x27;function. Instead received: %s.&#x27;</span>,</span><br><span class="line">      callback,</span><br><span class="line">    );</span><br><span class="line">    update.callback = callback;</span><br><span class="line">  &#125;</span><br><span class="line">  <span class="comment">// 把更新的对象加入到 fiber 对象上的 updateQueue 里， 会有很多更新在一个节点上产生</span></span><br><span class="line">  enqueueUpdate(current, update);</span><br><span class="line"></span><br><span class="line">  <span class="comment">//进行任务调度</span></span><br><span class="line">  <span class="comment">//当React进行Update后，就要进行调度</span></span><br><span class="line">  <span class="comment">//即 根据任务的优先级去调度任务</span></span><br><span class="line">  <span class="comment">//先执行优先级高的任务，</span></span><br><span class="line">  scheduleWork(current, expirationTime);</span><br><span class="line">  <span class="keyword">return</span> expirationTime;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>


    </div>

    
    
    

      <footer class="post-footer">
          <div class="post-tags">
              <a href="/hexo/tags/React/" rel="tag"># React</a>
              <a href="/hexo/tags/%E6%BA%90%E7%A0%81/" rel="tag"># 源码</a>
          </div>

        


        
    <div class="post-nav">
      <div class="post-nav-item">
    <a href="/hexo/2020/09/01/canvas-%E7%A6%BB%E5%B1%8F%E6%8A%80%E6%9C%AF/" rel="prev" title="canvas-离屏技术">
      <i class="fa fa-chevron-left"></i> canvas-离屏技术
    </a></div>
      <div class="post-nav-item">
    <a href="/hexo/2020/09/14/react%E6%BA%90%E7%A0%81-react-fiber-root/" rel="next" title="react源码-react-fiber-root">
      react源码-react-fiber-root <i class="fa fa-chevron-right"></i>
    </a></div>
    </div>
      </footer>
    
  </article>
  
  
  



          </div>
          

<script>
  window.addEventListener('tabs:register', () => {
    let { activeClass } = CONFIG.comments;
    if (CONFIG.comments.storage) {
      activeClass = localStorage.getItem('comments_active') || activeClass;
    }
    if (activeClass) {
      let activeTab = document.querySelector(`a[href="#comment-${activeClass}"]`);
      if (activeTab) {
        activeTab.click();
      }
    }
  });
  if (CONFIG.comments.storage) {
    window.addEventListener('tabs:click', event => {
      if (!event.target.matches('.tabs-comment .tab-content .tab-pane')) return;
      let commentClass = event.target.classList[1];
      localStorage.setItem('comments_active', commentClass);
    });
  }
</script>

        </div>
          
  
  <div class="toggle sidebar-toggle">
    <span class="toggle-line toggle-line-first"></span>
    <span class="toggle-line toggle-line-middle"></span>
    <span class="toggle-line toggle-line-last"></span>
  </div>

  <aside class="sidebar">
    <div class="sidebar-inner">

      <ul class="sidebar-nav motion-element">
        <li class="sidebar-nav-toc">
          Table of Contents
        </li>
        <li class="sidebar-nav-overview">
          Overview
        </li>
      </ul>

      <!--noindex-->
      <div class="post-toc-wrap sidebar-panel">
          <div class="post-toc motion-element"><ol class="nav"><li class="nav-item nav-level-3"><a class="nav-link" href="#ReactDOM-render"><span class="nav-number">1.</span> <span class="nav-text">ReactDOM.render</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#1%E3%80%81legacyRenderSubtreeIntoContainer"><span class="nav-number">1.1.</span> <span class="nav-text">1、legacyRenderSubtreeIntoContainer</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#1-1-legacyCreateRootFromDOMContainer"><span class="nav-number">1.2.</span> <span class="nav-text">1.1 legacyCreateRootFromDOMContainer</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#1-1-1-shouldHydrateDueToLegacyHeuristic"><span class="nav-number">1.3.</span> <span class="nav-text">1.1.1 shouldHydrateDueToLegacyHeuristic</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#1-1-1-1-ROOT-ATTRIBUTE-NAME"><span class="nav-number">1.4.</span> <span class="nav-text">1.1.1.1 ROOT_ATTRIBUTE_NAME</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#1-2-getPublicRootInstance"><span class="nav-number">1.5.</span> <span class="nav-text">1.2 getPublicRootInstance</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#1-3-ReactRoot-render"><span class="nav-number">1.6.</span> <span class="nav-text">1.3 ReactRoot.render</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#1-3-1-updateContainer"><span class="nav-number">1.7.</span> <span class="nav-text">1.3.1 updateContainer</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#1-3-1-1-updateContainerAtExpirationTime"><span class="nav-number">1.8.</span> <span class="nav-text">1.3.1.1 updateContainerAtExpirationTime</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#1-3-1-1-1-scheduleRootUpdate"><span class="nav-number">1.9.</span> <span class="nav-text">1.3.1.1.1 scheduleRootUpdate</span></a></li></ol></li></ol></div>
      </div>
      <!--/noindex-->

      <div class="site-overview-wrap sidebar-panel">
        <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
    <img class="site-author-image" itemprop="image" alt="幽辰"
      src="/hexo/images/avatar.jpg">
  <p class="site-author-name" itemprop="name">幽辰</p>
  <div class="site-description" itemprop="description"></div>
</div>
<div class="site-state-wrap motion-element">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
          <a href="/hexo/archives/">
        
          <span class="site-state-item-count">127</span>
          <span class="site-state-item-name">posts</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
            <a href="/hexo/categories/">
          
        <span class="site-state-item-count">14</span>
        <span class="site-state-item-name">categories</span></a>
      </div>
      <div class="site-state-item site-state-tags">
            <a href="/hexo/tags/">
          
        <span class="site-state-item-count">23</span>
        <span class="site-state-item-name">tags</span></a>
      </div>
  </nav>
</div>



      </div>

    </div>
  </aside>
  <div id="sidebar-dimmer"></div>


      </div>
    </main>

    <footer class="footer">
      <div class="footer-inner">
        

        

<div class="copyright">
  
  &copy; 
  <span itemprop="copyrightYear">2021</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">幽辰</span>
</div>
  <div class="powered-by">Powered by <a href="https://hexo.io/" class="theme-link" rel="noopener" target="_blank">Hexo</a> & <a href="https://theme-next.org/" class="theme-link" rel="noopener" target="_blank">NexT.Gemini</a>
  </div>

        








      </div>
    </footer>
  </div>

  
  <script src="/hexo/lib/anime.min.js"></script>
  <script src="/hexo/lib/velocity/velocity.min.js"></script>
  <script src="/hexo/lib/velocity/velocity.ui.min.js"></script>

<script src="/hexo/js/utils.js"></script>

<script src="/hexo/js/motion.js"></script>


<script src="/hexo/js/schemes/pisces.js"></script>


<script src="/hexo/js/next-boot.js"></script>




  















  

  

</body>
</html>
